#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define D long double
#define N 400005
#define lbt(x) (x&(-x))
int a[N];
int dp[202][202][202],qz[202][202][202],hz[202][202][202];
inline void chag(int x,int v,int ta[212]){
	x++;
	while(x<=202){
		ta[x]=max(ta[x],v);x+=lbt(x);
	}
}
inline int ask(int x,int ta[212]){
	x++;
	int mx=-1e9;
	while(x){
		mx=max(mx,ta[x]);x-=lbt(x);
	}
	return mx;
}
signed main(){
	freopen("shung.in","r",stdin);
	freopen("shung.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	//if(n<=2000){
	//memset(dp,-0x3f,sizeof(dp));//memset(hz,-0x3f,sizeof(hz));memset(qz,-0x3f,sizeof(qz));
	//dp[0][1][n]=0;
	//for(int i=n;i>=1;i--)	hz[0][1][i]=0;
	//for(int i=1;i<=n;i++)	qz[0][i][n]=0;
	int ans=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				dp[i][j][k]=dp[i-1][j][k];
			}
		}
		for(int j=1;j<=n;j++){
			for(int k=0;k<j;k++){
				dp[i][j][a[i]]=max(dp[i][j][a[i]],dp[i-1][k][a[i]]+1);
			}
		}
		for(int j=1;j<=n;j++){
			for(int k=j+1;k<=n+1;k++){
				dp[i][a[i]][j]=max(dp[i][a[i]][j],dp[i-1][a[i]][k]+1);
			}
		}
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				ans=max(ans,dp[i][j][k]);
			}
		}
	}
	cout<<ans;
	//}
	return 0;
}
/*#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define D long double
#define N 400005
#define lbt(x) (x&(-x))
int a[N];
int dp[2003][2003],ta[2003][2003],mx[2003],ta1[2003];
inline void chag(int x,int v,int ta[2003]){
	x++;
	while(x<=2002){
		ta[x]=max(ta[x],v);x+=lbt(x);
	}
}
inline int ask(int x,int ta[2003]){
	x++;
	int mx=-1e9;
	while(x){
		mx=max(mx,ta[x]);x-=lbt(x);
	}
	return mx;
}inline void chag(int x,int v){
	x++;
	while(x<=2002){
		ta1[x]=max(ta1[x],v);x+=lbt(x);
	}
}
inline int ask(int x){
	x++;
	int mx=-1e9;
	while(x){
		mx=max(mx,ta1[x]);x-=lbt(x);
	}
	return mx;
}
signed main(){
	freopen("shung.in","r",stdin);
	freopen("shung.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	if(n<=2000){
		for(int i=0;i<=2001;i++)	dp[0][i]=-1e9;
		memset(ta,-0x3f,sizeof(ta));memset(ta1,-0x3f,sizeof(ta1));
		chag(0,0);
		int ans=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++)	dp[i][j]=dp[i-1][j];
			dp[i][a[i]]=max(dp[i][a[i]],ask(2001-a[i])+1);//down
			for(int j=1;j<=n;j++){
				dp[i][j]=max(dp[i][j],ask(a[i],ta[j])+1);//up
			}
			for(int j=1;j<=n;j++){
				chag(2001-j,dp[i][j]);chag(a[i],dp[i][j],ta[j]);
				ans=max(ans,dp[i][j]);
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				cerr<<setw(3)<<((dp[i][j]<0)?-1:dp[i][j]);
			}cerr<<endl;
		}cerr<<endl;
		cout<<ans;
		return 0;
	}
	return 0;
}*/

